package cfca.seal.web.tag;

import cfca.seal.dao.main.ISealCertDao;
import cfca.seal.system.Env;
import cfca.seal.web.tag.base.TemplateDirectiveModelBase;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class SwKeyIndexSelectTag extends TemplateDirectiveModelBase
{

  @Resource
  ISealCertDao sealCertdao;

  public String process(Environment env, Map paramsMap, TemplateModel[] loopVars, TemplateDirectiveBody body)
    throws TemplateException, IOException
  {
    List listKeyId = new ArrayList();
    try
    {
      int swKeyIndexMax = Integer.parseInt(Env.swKeyIndexMax);
      for (int i = 0; i < swKeyIndexMax; i++) {
        listKeyId.add(String.valueOf(i + 1));
      }

      List listKeyIdUsed = this.sealCertdao.getSealCertKeyIDForSelectShow();

      if ((null != listKeyIdUsed) && (listKeyIdUsed.size() > 0))
        listKeyId.removeAll(listKeyIdUsed);
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    StringBuffer sf = new StringBuffer();
    sf.append("<select size=\"1\" class=\"");
    sf.append(this.className);
    sf.append("\" id=\"");
    sf.append(this.elementId);
    sf.append("\" name = \"");
    sf.append(this.elementName).append("\" >");
    sf.append("<option value=\"\">--请选择--</option>");

    for (int i = 0; i < listKeyId.size(); i++) {
      String keyId = (String)listKeyId.get(i);
      sf.append("<option value=\"").append(keyId).append("\" ");
      sf.append(" >").append(keyId).append("</option>");
    }
    sf.append("</select>");
    return sf.toString();
  }
}